package com.aistar.util;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

/**
 * 连接与关闭SqlSession
 */
public class MybatisUtil {

    private static SqlSessionFactory sqlSessionFactory;
    private static SqlSession sqlSession;

    static{
        InputStream is = MybatisUtil.class.getClassLoader().getResourceAsStream("../resource/config/mybatis-config.xml");
        sqlSessionFactory =   new SqlSessionFactoryBuilder().build(is);
    }

    public static SqlSession getSqlSession(){
            sqlSession = sqlSessionFactory.openSession();
            return sqlSession;

    }

    public static void close(){
        if(sqlSession != null)
            sqlSession.close();
    }


}
